package demo;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TaskTest {
	private static final Logger LOG = LoggerFactory.getLogger(TaskTest.class);

	ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

	/**
	 * 部署流程定义
	 */
	@Test
	public void deploy() {
		InputStream inputStreamBpmn = this.getClass().getResourceAsStream("MyProcess.bpmn");
		InputStream inputStreamPng = this.getClass().getResourceAsStream("MyProcess.png");

		Deployment deployment = processEngine.getRepositoryService()// 与流程定义和部署对象相关的Service
				.createDeployment() // 创建一个部署对象
				.name("测试")// 添加部署的名称
				.addInputStream("MyProcess.bpmn", inputStreamBpmn)//
				.addInputStream("MyProcess.png", inputStreamPng)//
				.deploy(); // 完成部署
		System.out.println("部署ID:" + deployment.getId()); //
		System.out.println("部署名称:" + deployment.getName()); //
	}

	/**启动流程实例**/
	@Test
	public void startProcessInstance(){
		//流程定义的key
		String processDefinitionKey ="myProcess";
		ProcessInstance pi = processEngine.getRuntimeService()//与正在执行	的流程实例和执行对象相关的Service
						.startProcessInstanceByKey(processDefinitionKey);  //使用流程定义的key启动流程实例,key对应helloworld.bpmn文件中id的属性值，使用key值启动，默认是按照最新版本的流程定义启动
		System.out.println("流程实例ID:"+pi.getId());//流程实例ID   101
		System.out.println("流程定义ID:"+pi.getProcessDefinitionId());//流程定义ID
	}

	/**查询当前人的个人任务*/
	@Test
	public void findFirstTask(){
		String assignee = "刘少华";
		 List<Task> list = processEngine.getTaskService()//与正在执行的任务管理相关的Service
						.createTaskQuery()//创建任务查询
						.taskAssignee(assignee)//指定个人任查询，指定办理人
						.list();
		if(list!=null && list.size()>0){
			for(Task task:list){
				System.out.println("任务ID:"+task.getId());
				System.out.println("任务名称:"+task.getName());
				System.out.println("任务的创建时间:"+task.getCreateTime());
				System.out.println("任务的办理人:"+task.getAssignee());
				System.out.println("流程实例ID:"+task.getProcessInstanceId());
				System.out.println("执行对象ID:"+task.getExecutionId());
				System.out.println("流程定义ID:"+task.getProcessDefinitionId());
				System.out.println("############################################");
			}
		}
	}

	/**完成我的任务*/
	@Test
	public void completeFirstTask(){
		//任务ID
		String taskId = "2509";
		processEngine.getTaskService()//与正在执行的任务管理相关的Service
						.complete(taskId);
		System.out.println("完成任务：任务ID:"+taskId);
	}

	/**查询当前人的个人任务*/
	@Test
	public void findMyPersonalTask(){
		String assignee = "杨恒涛";
		 List<Task> list = processEngine.getTaskService()//与正在执行的任务管理相关的Service
						.createTaskQuery()//创建任务查询
						.taskAssignee(assignee)//指定个人任查询，指定办理人
						.list();
		if(list!=null && list.size()>0){
			for(Task task:list){
				System.out.println("任务ID:"+task.getId());
				System.out.println("任务名称:"+task.getName());
				System.out.println("任务的创建时间:"+task.getCreateTime());
				System.out.println("任务的办理人:"+task.getAssignee());
				System.out.println("流程实例ID:"+task.getProcessInstanceId());
				System.out.println("执行对象ID:"+task.getExecutionId());
				System.out.println("流程定义ID:"+task.getProcessDefinitionId());
				System.out.println("############################################");
			}
		}
	}

	/**完成我的任务*/
	@Test
	public void completeMyPersonalTask(){
		//任务ID
		String taskId = "5002";
		processEngine.getTaskService()//与正在执行的任务管理相关的Service
						.complete(taskId);
		System.out.println("完成任务：任务ID:"+taskId);
		Task task = processEngine.getTaskService().createTaskQuery().singleResult();
		System.out.println("流程结束后查找任务：" + task);
		processEngine.close();
	}
	
	public int[] twoSum(int[] numbers, int target) {
		Map<Integer, Integer> map = new HashMap<>();
		for (int i = 0; i < numbers.length; i++) {
			if (map.containsKey(target - numbers[i])) {
				return new int[]{map.get(target - numbers[i]), i};
			}
			map.put(numbers[i], i);
		}
		return new int[]{0, 0};
	}

	@Test
	public void ceshi() {
		int[] a = {1, 2, 3, 4};
		int[] b = twoSum(a, 10);
		System.out.println(b);
	}

}
